package cn.smartorigin.soas.frame;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;

import cn.smartorigin.soas.data.SqliteConnecter;
import cn.smartorigin.soas.data.TestData;
import cn.smartorigin.soas.frame.panel.ProductListPage;

public class AddProduct extends JFrame{

	private static final long serialVersionUID = 1L;
	//MainFrame mf = null;
	JButton mCfnBtn, mClrBtn;
	//JComboBox mClientName;
	JTextField mClientName;
	JTextField mProductName, mProductPrice;
	int clientId;
	String mClientNameStr;
	JComboBox mPlateKind, mPlateType;
	
	ProductListPage plp;
	
	
	public AddProduct( int id ,String cname, ProductListPage pl){
		super();
		//mf = mainframe;
		this.plp = pl;
		this.clientId = id;
		this.mClientNameStr = cname;
		this.setTitle(" 添加产品  - SOAS");
		this.setIconImage(new ImageIcon("img/sosa_logo.png").getImage());
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setSize(600,400);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.initBackGround();
		this.initFont();
		this.compentInit();
		this.setVisible(true);
	}
	
	private void initBackGround(){	  
		  ImagePanel ip = new ImagePanel(new ImageIcon("res/images/dialog_bg.png"));
		  this.setContentPane(ip); 
	}
	
	private void initFont(){
		Font font = new Font("宋体", Font.PLAIN, 12);
		UIManager.put("Button.font", font);
		UIManager.put("Label.font", font);
		UIManager.put("ComboBox.font", font);

	}
	
	private void compentInit(){
		this.setLayout(null);
		BtnListener btnlistener = new BtnListener();
		
		ImageIcon clrCfnIcon=new ImageIcon("img/corfim.png");
		mCfnBtn = new JButton("确定", clrCfnIcon);
		mCfnBtn.setBounds(250,300,100,30);
		mCfnBtn.addActionListener(btnlistener);
		
		ImageIcon clrBtnIcon=new ImageIcon("img/ic_delete.png"); 
		mClrBtn = new JButton("取消",clrBtnIcon);
		mClrBtn.setBounds(450,300,100,30);
		mClrBtn.addActionListener(btnlistener);
		
		JLabel clientName  = new JLabel("客户名称：");
		clientName.setBounds(250,20,100,30);
		//mClientName = new JComboBox(loadClientData());
		//mClientName.setBounds(350,50,200,30);
		//mClientName.setBackground(Color.white);
		mClientName = new JTextField(mClientNameStr);
		mClientName.setBounds(350,20,200,30);
		mClientName.setBackground(Color.white);
		mClientName.addActionListener(btnlistener);
		mClientName.setEditable(false);
		
		JLabel productNamelbl  = new JLabel("材料名称：");
		productNamelbl.setBounds(250,70,100,30);
		mProductName = new JTextField();
		mProductName.setBounds(350,70,200,30);
		
		JLabel plateTypeLbl  = new JLabel("表面处理方式：");
		plateTypeLbl.setBounds(250,120,100,30);
		try {
			mPlateType = new JComboBox(	TestData.queryPlateType(new SqliteConnecter()));
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mPlateType.setBounds(350,120,100,30);
		mPlateType.setBackground(Color.white);
		
		JLabel plateKindLbl  = new JLabel("镀种：");
		plateKindLbl.setBounds(250,170,100,30);
		String item[] = {"滚镀", "吊镀"};
		mPlateKind = new JComboBox(item);
		mPlateKind.setBounds(350,170,100,30);
		mPlateKind.setBackground(Color.white);
		
		JLabel productPricelbl  = new JLabel("单价：");
		productPricelbl.setBounds(250,220,100,30);
		mProductPrice = new  JTextField();
		mProductPrice.setBounds(350,220,50,30);
		
		this.add(mCfnBtn);
		this.add(mClrBtn);
		this.add(mClientName);
		this.add(clientName);
		this.add(productNamelbl);
		this.add(mProductName);
		this.add(productPricelbl);
		this.add(mProductPrice);
		this.add(plateTypeLbl);
		this.add(plateKindLbl);
		this.add(mPlateKind);
		this.add(mPlateType);
		
	}
	
//	private  Vector<Object> loadClientData(){
//
//		try {
//			Vector<Object> v;
//			v = TestData.queryClientList(new SqliteConnecter());
//			return v;
//		} catch (ClassNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return null;
//	}

	private  void frameClose(){
		this.dispose();
	}
	
	private void saveData(){
		//String str1 = mClientName.getSelectedItem().toString();
		String str2 = mProductName.getText();
		String str3 = mProductPrice.getText();
		String str4 = mPlateType.getSelectedItem().toString();
		String str5 = mPlateKind.getSelectedItem().toString();
		if(str2.isEmpty()){
			JOptionPane.showMessageDialog(null, "名称不能为空", "警告", JOptionPane.WARNING_MESSAGE);
		}else if(str3.isEmpty()){
			JOptionPane.showMessageDialog(null, "价格不能为空", "警告", JOptionPane.WARNING_MESSAGE);
		}else{
			try {
				TestData.insertProduct(new SqliteConnecter(), str2, str3, str4, str5, clientId);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
			plp.flashData();
			frameClose();
		}

	}
	
	class BtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			Object  obj =(Object)e.getSource();
			if(obj == mCfnBtn){
				saveData();

			}else if(obj == mClrBtn){
				frameClose();
			}
		}
	}

}
